package com.mingyizhudao.app.view.discover;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import com.mingyizhudao.app.R;
import com.mingyizhudao.app.utils.IntentHelper;
import com.mingyizhudao.app.view.MYBaseFragment;
import com.mingyizhudao.app.view.WebViewActivity;

public class DiscoverFragment extends MYBaseFragment{
    private WebView advertise_webview;
    private ProgressBar progressBar;
    private String title;
    private boolean isFirst = true;
    private final String URL = "http://m.mingyizhudao.com/mobile/event/index/header/0/app/0/page/";
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
	}

	@Override
	public void onResume() {
		super.onResume();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View mView = inflater.inflate(R.layout.web_layout, container,
				false);
		advertise_webview = (WebView) mView.findViewById(R.id.advertise_webview);
	    progressBar = (ProgressBar) mView.findViewById(R.id.progress_bar);
	    
        advertise_webview.setHorizontalScrollBarEnabled(false);
        advertise_webview.setVerticalScrollBarEnabled(false);
        //填充webview
        advertise_webview.setNetworkAvailable(true);
        advertise_webview.getSettings().setJavaScriptEnabled(true);
        advertise_webview.getSettings().setDefaultTextEncodingName("utf-8");
        
        advertise_webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                view.loadUrl(url);
//                Log.e("====", "url = "+url);
            	IntentHelper.getInstance(activity).gotoActivityWithURL(WebViewActivity.class, url);
                return true;
            }
            
        });
        
        advertise_webview.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int progress) {
                progressBar.setProgress(progress);
                if (progress >= 100) {
                    progressBar.setVisibility(View.GONE);
                }
            }
        });
        
        advertise_webview.loadUrl(URL+0);
		return mView;
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void initHeaderView() {
		clearTitle();
		setTopViewBg(R.color.actionbar_bg_color);
		View view = activity.getLayoutInflater().inflate(R.layout.title_discover, null);
		RadioGroup rg = ((RadioGroup)view.findViewById(R.id.grdiogroup));
		if(!isFirst){
			((RadioButton)rg.getChildAt(1)).setChecked(true);
		}
		rg.setOnCheckedChangeListener(onCheckedChangeListener);
		initCenterView(view);
	}

	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		if (!hidden) {
			if(!isFirst){
				setProgress();
				advertise_webview.loadUrl(URL+1);
			}else {
				setProgress();
			   advertise_webview.loadUrl(URL+0);
			}
		}
		
	}
	private OnCheckedChangeListener onCheckedChangeListener = new OnCheckedChangeListener(){

		@Override
		public void onCheckedChanged(RadioGroup arg0, int arg1) {
			int id = arg0.getCheckedRadioButtonId();
			switch (id) {
				case R.id.rb1:
					setProgress();
					advertise_webview.loadUrl(URL+0);
					isFirst = true;
					break;
				case R.id.rb2:
					setProgress();
					advertise_webview.loadUrl(URL+1);
					isFirst = false; 
					break;
			}
		}
		
	};
	
	public void setProgress() {
		progressBar.setVisibility(View.VISIBLE);
		 advertise_webview.setWebChromeClient(new WebChromeClient() {
	         @Override
	         public void onProgressChanged(WebView view, int progress) {
	             progressBar.setProgress(progress);
	             if (progress >= 100) {
	                 progressBar.setVisibility(View.GONE);
	             }
	         }
	     });
	}
	
}
